

package com.bei_en.teacher.admin.controller.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bei_en.teacher.admin.model.sys.bean.SysConstant;
import com.bei_en.teacher.admin.model.sys.condition.SysConstantCondition;
import com.bei_en.teacher.admin.model.sys.vo.SysConstantVo;
import com.bei_en.teacher.admin.model.questions.vo.QuestionsVo;
import com.bei_en.teacher.admin.service.sys.SysConstantService;
import com.bei_en.teacher.admin.controller.BaseController;
import com.bei_en.common.web.JsonFailResult;
import com.bei_en.common.web.JsonResult;
import com.bei_en.common.web.JsonSuccessResult;
import com.mysql.fabric.xmlrpc.base.Array;
/**
 * 
 * @author Guocg
 * @date 2016-11-26 9:43:16
 */
@Controller
@RequestMapping("/api/SysConstant")
public class SysConstantAPPController extends BaseController {

    @Autowired
    SysConstantService sysConstantService;


    @RequestMapping("/sysConstant")
	@ResponseBody
    public JsonResult sysConstant(Model model,@Valid  SysConstantCondition vo, BindingResult br, HttpSession session ) {
    	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
    	
     	try {
     		
     	   List<SysConstantVo> list =new ArrayList<SysConstantVo>();
		   list = sysConstantService.findSysConstantPage(vo);
		   JSONArray array=new JSONArray();
		   JSONObject object=null;
		   SysConstantVo sysConstantVo=null;
		   for (int i = 0; i < list.size(); i++) {
			   object=new JSONObject();
			   sysConstantVo=list.get(i);
	   seetingJsonObject(object,"id",  sysConstantVo.getId());
	   seetingJsonObject(object,"name",  sysConstantVo.getName());
	   seetingJsonObject(object,"value",  sysConstantVo.getValue());
	   seetingJsonObject(object,"type",  sysConstantVo.getType());
//	   seetingJsonObject(object,"updateId",  sysConstantVo.getUpdateId());
//	   seetingJsonObject(object,"remarks",  sysConstantVo.getRemarks());
	   if(sysConstantVo.getUpdateTime()!=null){
		   seetingJsonObject(object,"updateTime",  sysConstantVo.getUpdateTime().getTime());
	   }
	   if(sysConstantVo.getCreateTime()!=null){
		   seetingJsonObject(object,"createTime",  sysConstantVo.getCreateTime().getTime());
	   }
	   
			   array.add(object);
		}
		return new JsonSuccessResult(session.getId(), array);
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
	 
    
    @RequestMapping("/sysConstantAdd")
    @ResponseBody
    public JsonResult sysConstantAdd(Model model,@Valid  SysConstantCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	try {
      		
      	
		    SysConstantVo sysConstant=new SysConstantVo();
		    
	   sysConstant.setId(vo.getId());
	   sysConstant.setName(vo.getName());
	   sysConstant.setValue(vo.getValue());
	   sysConstant.setType(vo.getType());
	   sysConstant.setUpdateId(vo.getUpdateId());
	   sysConstant.setRemarks(vo.getRemarks());
	   sysConstant.setUpdateTime(vo.getUpdateTime());
	   sysConstant.setCreateTime(vo.getCreateTime());
		    
			sysConstantService.insert(sysConstant);
			return new JsonSuccessResult(session.getId(), "成功");
      	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }
    @RequestMapping("/sysConstantEdit")
    public JsonResult sysConstantEdit(Model model,@Valid  SysConstantCondition vo, BindingResult br, HttpSession session) {
      	if (br.hasErrors())
       		return new JsonFailResult(session.getId(), br.getAllErrors().get(0)
					.getDefaultMessage());
      	
    	try {
    	SysConstantVo sysConstant=new SysConstantVo();
	
		sysConstantService.updateSelective(sysConstant);
	
		return new JsonSuccessResult(session.getId(), "成功");
		
     	} catch (Exception e) {
			e.printStackTrace();
			return new JsonFailResult(session.getId(), 1, "后台报错",
					e.getMessage());
		}
    }

  
    
}
